<template>
  <div
    class="app-container"
    v-loading="AllLoading"
    element-loading-text="资源包加载更新中(3-5分钟)，请勿离开此窗口"
    element-loading-spinner="el-icon-loading"
  >
    <div>
      <el-button type="primary" @click="addJbbhmb(0)"
        >新增基本笔画模板</el-button
      >
      <el-button type="primary" @click="addZiyuan()">更新资源包</el-button>
    </div>
    <el-table
      :data="jbbhmbList"
      stripe
      v-loading="jbbhLoading"
      style="width: 100%"
    >
      <el-table-column type="expand" padding="0">
        <template slot-scope="props">
          <el-table :data="props.row.basisStrokes" style="width: 100%">
            <el-table-column
              prop="word"
              header-align="center"
              align="center"
              label="字"
            ></el-table-column>
            <el-table-column
              prop="border"
              header-align="center"
              align="center"
              label="顺序"
            >
            </el-table-column>
            <el-table-column
              prop="wordVideo"
              header-align="center"
              align="center"
              label="字视频"
            >
              <template slot-scope="scope">
                <video
                  :src="scope.row.wordVideo"
                  controls="controls"
                  class="avatar"
                >
                  您的浏览器不支持视频播放
                </video>
              </template>
            </el-table-column>
            <el-table-column header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button style="" type="text" @click="addjbbh(1, scope.row)"
                  >修改</el-button
                >
                <el-button type="text" @click="deleteById(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>

      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="模板ID"
      ></el-table-column>
      <el-table-column
        prop="forworkName"
        header-align="center"
        align="center"
        label="模板名称"
      ></el-table-column>
      <el-table-column
        prop="forder"
        header-align="center"
        align="center"
        label="顺序"
      >
      </el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button style="" type="text" @click="addjbbh(0, scope.row)"
            >新增笔画</el-button
          >
          <el-button style="" type="text" @click="addJbbhmb(1, scope.row.id)"
            >修改模板</el-button
          >
          <el-button type="text" @click="deleteJbbhmById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <el-dialog
      :visible.sync="dialogjbbhVisible"
      :title="type == 0 ? '添加基本笔画视频' : '修改基本笔画视频'"
    >
      <el-form
        :rules="dataRule"
        ref="jbbhForm"
        :model="jbbh"
        label-width="120px"
      >
        <el-form-item label="字" prop="word" style="width: 300px">
          <el-input v-model="jbbh.word" />
        </el-form-item>
        <el-form-item label="顺序" prop="border">
          <el-input-number
            v-model="jbbh.border"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="基本笔画视频" prop="wordVideo">
          <el-upload
            class="upload-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :on-success="handleAudioSuccess"
            :before-upload="beforeVideoUpload"
            :show-file-list="false"
          >
            <video :src="jbbh.wordVideo" controls="controls" class="avatar">
              您的浏览器不支持视频频播放
            </video>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogjbbhVisible = false">取 消</el-button>
        <el-button
          :disabled="savejbbhBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogJbbhmbVisible"
      :title="type == 0 ? '添加基本笔画模板' : '修改基本笔画模板'"
    >
      <el-form
        :rules="dataRule"
        ref="JbbhmbForm"
        :model="Jbbhmb"
        label-width="120px"
      >
        <el-form-item label="模板名称" prop="forworkName" style="width: 300px">
          <el-input v-model="Jbbhmb.forworkName" />
        </el-form-item>
        <el-form-item label="顺序" prop="forder">
          <el-input-number
            v-model="Jbbhmb.forder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogJbbhmbVisible = false">取 消</el-button>
        <el-button
          :disabled="savejbbhBtnDisabled"
          type="primary"
          @click="saveOrUpdateJbbhmb"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      buzhou: 0,
      jbbhLoading: false,
      AllLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      Jbbhmb: {
        id: "",
        forder: 0,
        forworkName: "",
      },
      jbbh: {
        fid: "",
        word: "",
        wordVideo: "",
        border: 1,
        id: null,
      },
      jbbhmbList: [],
      jbbhList: [],
      type: 0, // 0:添加1:修改
      xzBtn: false,
      dialogJbbhmbVisible: false,
      dialogjbbhVisible: false,
      savejbbhBtnDisabled: false,
      dataRule: {
        word: [
          {
            required: true,
            message: "字不能为空",
            trigger: "blur",
          },
          {
            max: 1,
            message: "字长度为1",
          },
        ],
        wordVideo: [
          {
            required: true,
            message: "视频不能为空",
          },
        ],
        forder: [
          {
            required: true,
            message: "顺序不能为空",
            trigger: "blur",
          },
        ],
        border: [
          {
            required: true,
            message: "顺序不能为空",
            trigger: "blur",
          },
        ],
        forworkName: [
          {
            required: true,
            message: "模板名称不能为空",
            trigger: "blur",
          },
        ],
      },
      pageIndex: 1, // 当前页
      pageSize: 10, // 每页数据显示几条
      totalPage: 0, // 总记录数
    };
  },

  activated() {
    // 有数据就要回显数据
    this.getJbbhmbList();
  },

  methods: {
    beforeVideoUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp4" && fileType !== ".flv") {
        this.$message.error("上传视频频只能是mp4/flv!");
        return false;
      }
      return true;
    },
    //上传成功后的回调函数
    handleAudioSuccess(response) {
      this.jbbh.wordVideo = response.url;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getJbbhmbList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getJbbhmbList();
    },
    // 获取基本笔画模板列表
    getJbbhmbList() {
      this.jbbhLoading = true;
      this.$http({
        url: this.$http.adornUrl(`/sf/cbbasisstrokesformwork/getJbbhmbList`),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.jbbhmbList = data.jbbhmbPage.records;
          this.totalPage = data.jbbhmbPage.total;
          this.xzBtn = true;
        } else {
          this.jbbhmbList = [];
          this.totalPage = 0;
        }
        this.jbbhLoading = false;
      });
    },
    getJbbhList() {
      this.jbbhLoading = true;
      this.$http({
        url: this.$http.adornUrl(`/sf/cbbasisstrokes/getJbbhList`),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.jbbhList = data.jbbhPage.records;
          this.totalPage = data.jbbhPage.total;
          this.xzBtn = true;
        } else {
          this.jbbhList = [];
          this.totalPage = 0;
        }
        this.jbbhLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.jbbhFormSubmit();
      } else {
        // 修改章节
        this.updatejbbh();
      }
    },
    saveOrUpdateJbbhmb() {
      if (this.type == 0) {
        // 添加章节
        this.JbbhmbFormSubmit();
      } else {
        // 修改章节
        this.updateJbbhmb();
      }
    },
    // 更新章节图片信息
    updatejbbh() {
      this.$refs["jbbhForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbbasisstrokes/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.jbbh.id,
                border: this.jbbh.border,
                word: this.jbbh.word,
                wordVideo: this.jbbh.wordVideo,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getJbbhmbList();
                  this.dialogjbbhVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    updateJbbhmb() {
      this.$refs["JbbhmbForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbbasisstrokesformwork/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.Jbbhmb.id,
                forder: this.Jbbhmb.forder,
                forworkName: this.Jbbhmb.forworkName,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getJbbhmbList();
                  this.dialogJbbhmbVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addjbbh(status, row) {
      if (this.$refs.jbbhForm) this.$refs.jbbhForm.resetFields();
      // 重置表单 应该只是把表单有的清空而已
      if (status == 0) {
        // 添加
        this.type = 0;
        this.jbbh.fid = row.id;
        // 清空
        this.jbbh.id = null;
        this.jbbh.border = 1;
        this.jbbh.wordVideo = "";
        this.jbbh.word = "";
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.jbbh.id = row.id;
        this.jbbh.border = row.border;
        this.jbbh.word = row.word;
        this.jbbh.wordVideo = row.wordVideo;
        this.jbbh.fid = row.fid;
      }
      this.dialogjbbhVisible = true;
    },
    addJbbhmb(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      setTimeout(() => {
        this.$refs.JbbhmbForm.resetFields();
      }, 0);
      // this.$nextTick(() => {
      //     this.dialogJbbhmbVisible = true;
      //     this.$refs.JbbhmbForm.resetFields();
      // });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.Jbbhmb.id = null;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbbasisstrokesformwork/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.Jbbhmb.id = data.cbBasisStrokesFormwork.id;
          this.Jbbhmb.forder = data.cbBasisStrokesFormwork.forder;
          this.Jbbhmb.forworkName = data.cbBasisStrokesFormwork.forworkName;
        });
      }
      this.dialogJbbhmbVisible = true;
    },
    // 表单提交
    jbbhFormSubmit() {
      // 表单验证
      this.$refs["jbbhForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbbasisstrokes/save`),
            method: "post",
            data: this.$http.adornData(
              {
                fid: this.jbbh.fid,
                border: this.jbbh.border,
                word: this.jbbh.word,
                wordVideo: this.jbbh.wordVideo,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getJbbhmbList();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogjbbhVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 表单提交
    JbbhmbFormSubmit() {
      // 表单验证
      this.$refs["JbbhmbForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbbasisstrokesformwork/save`),
            method: "post",
            data: this.$http.adornData(
              {
                forder: this.Jbbhmb.forder,
                forworkName: this.Jbbhmb.forworkName,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getJbbhmbList();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogJbbhmbVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 通过章节id删除章节
    deleteById(id) {
      this.$confirm(`确定对[基本笔画id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbbasisstrokes/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getJbbhmbList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 通过模板id删除章节
    deleteJbbhmById(id) {
      this.$confirm(`确定对[基本笔画模板id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbbasisstrokesformwork/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getJbbhmbList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 更新资源包
    addZiyuan() {
      this.AllLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/cbbasisstrokesformwork/createJbbhmbZip"),
        method: "get",
      }).then(({ data }) => {
        if (data || data.code === 0) {
          this.$message({
            message: "资源包更新成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.AllLoading = false;
            },
          });
        } else {
          this.$message.error("资源包更新失败");
          this.AllLoading = false;
        }
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}

.chanpterList li {
  position: relative;
}

.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}

.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}

.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
